<?php
class PublicFunc
{
	/**
	 * @param string $server 服务器地址
	 * @param string $curlPost post数据，如：name=toy&age=22
	 * @return string 页面返回值
	 */
	public static function post($server, $curlPost)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $server);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 80);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}

	public static function gzip_encode($data)
	{
		if (strlen($data) >= 100 && $GLOBALS["serverCfg"]['gzipIsOpen']) {
			if (extension_loaded("zlib") && strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")) {
				ob_start("ob_gzhandler");
			}
			echo $data;
			if (extension_loaded("zlib")) {
				ob_end_flush();
			}
		} else {
			echo $data;
		}
		exit();
	}

	/**
	 * 获取毫秒级时间戳
	 * @return number
	 */
	public static function getMillisecond()
	{
		list($t1, $t2) = explode(' ', microtime());
		return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
	}

	/**
	 * 随机产生8位字符
	 * 随机原子:
	 * 1:大写字母
	 * 2:小写字母
	 * 3:数字
	 * @param mixed $maxlen 最大长度
	 * @param mixed $rand_begin 随机开始原子
	 * @param mixed $rand_end 随机结束原子
	 * @return string
	 */
	public static function randCharEight($maxlen = 8, $rand_begin = 1, $rand_end = 3)
	{
		$str = "";
		for ($i = 0; $i < $maxlen; $i++) {
			//1:大写字母
			//2:小写字母
			//3:数字
			$index = rand($rand_begin, $rand_end);
			$chr = "";
			switch ($index) {
				case 1:
					$chr = chr(rand(65, 90));
					break;
				case 2:
					$chr = chr(rand(97, 122));
					break;
				case 3:
					$chr = (string)rand(0, 9);
					break;
			}
			$str .= $chr;
		}
		return $str;
	}

	/**
	 * 检查手机号码合法性
	 * @param mixed $phone_num
	 * @return boolean
	 */
	public static function isPhoneNum($phone_num)
	{
		if (preg_match("/^1[23456789]\d{9}$/", $phone_num)) {
			return true;
		} else {
			return false;
		}
	}
}
